package com.ruoyi.project.lesson.banner.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lesson.banner.domain.LessonBanner;
import com.ruoyi.project.lesson.banner.service.ILessonBannerService;
import com.ruoyi.project.lesson.lesson.service.ILessonService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程bannerController
 *
 * @author shixiangwen
 * @date 2020-12-07
 */
@Controller
@RequestMapping("/lesson/banner")
public class LessonBannerController extends BaseController {

    private String prefix = "lesson/banner";

    @Autowired
    private ILessonBannerService lessonBannerService;

    @Autowired
    private ILessonService lessonService;

    @RequiresPermissions("lesson:banner:view")
    @GetMapping("/{lessonId}")
    public String banner(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lesson", lessonService.selectLessonById(lessonId));
        return prefix + "/banner";
    }

    /**
     * 查询课程banner列表
     */
    @RequiresPermissions("lesson:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LessonBanner lessonBanner) {
        startPage();
        List<LessonBanner> list = lessonBannerService.selectLessonBannerList(lessonBanner);
        return getDataTable(list);
    }

    /**
     * 导出课程banner列表
     */
    @RequiresPermissions("lesson:banner:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LessonBanner lessonBanner) {
        List<LessonBanner> list = lessonBannerService.selectLessonBannerList(lessonBanner);
        ExcelUtil<LessonBanner> util = new ExcelUtil<LessonBanner>(LessonBanner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 新增课程banner
     */
    @GetMapping("/add/{lessonId}")
    public String add(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lessonId", lessonId);
        mmap.put("lesson", lessonService.selectLessonById(lessonId));
        mmap.put("sort", lessonBannerService.getMaxSort(lessonId));
        return prefix + "/add";
    }

    /**
     * 新增保存课程banner
     */
    @RequiresPermissions("lesson:banner:add")
    @Log(title = "课程banner", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LessonBanner lessonBanner) {
        return toAjax(lessonBannerService.insertLessonBanner(lessonBanner));
    }

    /**
     * 修改课程banner
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        LessonBanner lessonBanner = lessonBannerService.selectLessonBannerById(id);
        mmap.put("lessonBanner", lessonBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程banner
     */
    @RequiresPermissions("lesson:banner:edit")
    @Log(title = "课程banner", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LessonBanner lessonBanner) {
        return toAjax(lessonBannerService.updateLessonBanner(lessonBanner));
    }

    /**
     * 删除课程banner
     */
    @RequiresPermissions("lesson:banner:remove")
    @Log(title = "课程banner", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lessonBannerService.deleteLessonBannerByIds(ids));
    }
}
